package com.jufa_3.controller;

import com.jufa_3.common.R;
import com.jufa_3.entity.CaseInfo;
import com.jufa_3.entity.Opinion;
import com.jufa_3.entity.UpInfo;
import com.jufa_3.service.CaseInfoService;
import com.jufa_3.service.ComboOrderService;
import com.jufa_3.service.OpinionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private CaseInfoService caseInfoService;

    @Autowired
    private OpinionService opinionService;

    @Autowired
    private ComboOrderService cos;

    @Autowired
    private StringRedisTemplate srt;

    private String[] upList = {"cqy", "crz", "csfgjb", "csf"};

    @GetMapping("/getRunParam")
    public R<List> getRunParam() {
        List<UpInfo> upInfos = new ArrayList<>();
        for (String upStr : upList) {
            upInfos.add(new UpInfo(Boolean.parseBoolean(srt.opsForValue().get(upStr)), srt.opsForValue().get(upStr + "Msg")));
        }
        return R.success(upInfos);
    }

    @GetMapping("/getUpData")
    public R<List> getUpData(String openId) {
        List<UpInfo> upInfos = new ArrayList<>();
        for (String upStr : upList) {
            if ("csfgjb".equals(upStr) && cos.without399Combo(openId)) {
                upInfos.add(new UpInfo(
                        true,
                        "此功能需充值 399尊惠版套餐 才能启用！"
                ));
                continue;
            }
            upInfos.add(new UpInfo(
                    Boolean.parseBoolean(srt.opsForValue().get(upStr)),
                    srt.opsForValue().get(upStr + "Msg")
            ));
        }
        return R.success(upInfos);
    }

    @GetMapping("/onLineContr/{key}/{value}")
    public void onLineController(@PathVariable("key") String key, @PathVariable("value") Boolean value) {
        srt.opsForValue().set(key, value.toString());
    }

    @GetMapping("getUserOpinions")
    public R<List<Opinion>> getUserOpinions(String openId, Boolean isLimit1) {
        return R.success(opinionService.lambdaQuery().
                eq(Opinion::getOpenId, openId).
                orderByDesc(Opinion::getCreateTime).
                last(isLimit1, "limit 1").
                last(!isLimit1, "limit 0,10").
                list());
    }

    @GetMapping("/updataOpinion")
    public R<Object> getUserOpinions(Opinion opinion) {
        return R.success(opinionService.updateById(opinion));
    }

    @GetMapping("/getTop20CaseInfo")
    public R<List<CaseInfo>> getTop20CaseInfo() {
        return R.success(caseInfoService.lambdaQuery().orderByDesc(CaseInfo::getEndDate).last("LIMIT 0 ,20").list());
    }

    @PostMapping("addUserOpinion")
    public R<Object> addUserOpinion(Opinion opinion) {
        if (StringUtils.isNotEmpty(srt.boundValueOps(opinion.getOpenId()).get())) {
            return R.error(4013, "添加频繁，请稍后添加！");
        }
        srt.boundValueOps(opinion.getOpenId()).set("opinion", 10, TimeUnit.SECONDS);
        LocalDateTime now = LocalDateTime.now();
        opinion.setCreateTime(now);
        opinion.setUpdataTime(now);
        opinion.setReplyText("暂无");
        if (opinionService.save(opinion)) {
            return R.success("反馈成功！");
        } else {
            return R.error(4006, "错误！");
        }
    }

}
